﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.SiteAdmin;
using Jzy.Core.Utility;
using Jzy.Core.Common;
using Jzy.Core.Common.Web;
using Jzy.Core.Attach;
using Jzy.Core.Common.Basic;

using Jzy.Model.SiteAdmin;
using Jzy.Common;

namespace Jzy.Web.SiteAdmin.Attach
{
    public partial class List : PageList
    {
        private ArticleBO Article
        {
            get
            {
                return ArticleBO.Instance<ArticleBO>();
            }
        }
      
        private string _menuID;
        private string MenuID
        {
            get
            {
                if (_menuID == null)
                {
                    if (string.IsNullOrEmpty(Request["MenuID"]))
                    {
                        _menuID = (new MenuBO()).GetIDByCode(MenuCode).ToString();
                    }
                    else
                    {
                        _menuID = StrUtil.DecryptUrl(Request["MenuID"]);
                    }
                }
                return _menuID;
            }
        }


        private string MenuCode
        {
            get
            {
                return Request["MenuCode"];
            }
        }

        private string PageSize
        {
            get
            {
                return Request.QueryString["PageSize"];
            }
        }

        private string AttachName
        {
            get
            {
                return Request.QueryString["AttachName"];
            }
        }

        //private PagerSortSetting _setting;

        protected void Page_Load(object sender, EventArgs e)
        {
            //_setting = this.Footer1.InitPager(new BindViewHandler(BindView));

            if (!IsPostBack)
            {
                
                Pager.BindDrp(drpPager);

                if (!string.IsNullOrEmpty(this.PageSize))
                {
                    drpPager.SelectedValue = this.PageSize;
                }

                txtSearch.Text = this.AttachName;

                BindView();
                if (MenuID != null)
                {
                    lblMenuName.Visible = true;
                    lblMenuName.Text = (new MenuBO()).GetName(MenuID) + "&nbsp;";
                }
            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);
            

            JSUtil.SetTableHeight("list", 145);

        }


        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "文件");

            string url = "Edit.aspx";
            if (MenuID != null)
            {
                url += "?MenuID=" + MenuID;
            }

            lnkAdd.HRef = "Edit.aspx?MenuID=" + MenuID;        
          
            JSUtil.ClickBtn(lnkDelete, btnDelete);

            JSUtil.ShowForm(lnkAdd, "上传附件", null, "Edit.aspx?MenuID=" + this.MenuID, 360, 143);
        }

        private void BindView()
        {

            LVUtil.Init(lv, lvHeader);
            chkSelectAll.Checked = false;

            PagerSetting setting = new PagerSetting();
            setting.PageIndex = this.PageIndex;
            setting.PageSize = int.Parse(drpPager.SelectedValue);
          

           // Pager.SetPageSize(_setting, drpPager);


            IEnumerable<AttachInfo> list = Article.GetAttachList(int.Parse(MenuID));
            if (txtSearch.Text.Trim() != "")
            {
                list = list.Where(t => t.Name.Contains(txtSearch.Text.Trim()));
            }

            setting.RecordCount = list.Count();

            list = list.Skip(setting.SkipCount).Take(setting.PageSize);

            lblStatistic.Text = "[&nbsp;共<b style='color:red;'> " + setting.RecordCount + "</b>&nbsp;个附件&nbsp;]";

            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataBind();


            string prefix = GlobalVar.WebCurrPath + "List.aspx?PageSize=" + drpPager.SelectedValue + "&MenuID=" + StrUtil.EncryptUrl(MenuID) + "&AttachName=" + txtSearch.Text.Trim();
            
            litPager.Text = HtmlPager.Render(setting, prefix, true);

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }
            List<int> lstSelectIDs = StrUtil.SplitToInt(strSelectIDs);

            List<int> ids = Article.GetIDsByAttachIDs(lstSelectIDs);

            //要删除相关的图片和附件
            Article.Delete(ids);
            BindView();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void drpPager_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindView();
        }


        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                string strID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());
                HyperLink lnkDown = (HyperLink)e.Item.FindControl("lnkDown");
                HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
                lnkName.NavigateUrl = lnkDown.NavigateUrl = GlobalVar.WebAppPath + "Common/FileDownLoad.aspx?TableName=" + TableName.CmsArticleAttach + "&ID=" + strID;

                //dt.Columns.Add("Index");
                //dt.Columns.Add("DownloadUrl");
                //string strUrl = GlobalVar.WebAppPath + "Common/FileDownLoad.aspx?TableName=" + (int)_tableName + "&ID=";
                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                //    DataRow dr = dt.Rows[i];
                //    dr["Index"] = i + 1;
                //    dr["DownloadUrl"] = strUrl + StrUtil.EncryptUrl(dr["ID"].ToString());
                //}

            }
        }
    }
}
